<template>
  <!-- 新增弹框 -->
  <el-dialog
    :title="{ add: '新增企业', edit: '编辑企业' }[mode]"
    :visible.sync="isShow"
    width="600px"
    center
    @close="closeEvent"
  >
    <!-- 表单数据 -->
    <el-form label-width="80px" :model="form" :rules="rules" ref="form">
      <el-form-item label="企业编号" prop="eid">
        <el-input v-model.trim="form.eid"></el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="name">
        <el-input v-model.trim="form.name"></el-input>
      </el-form-item>
      <el-form-item label="企业简称" prop="short_name">
        <el-input v-model.trim="form.short_name"></el-input>
      </el-form-item>
      <el-form-item label="企业简介" prop="intro">
        <el-input v-model.trim="form.intro"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model.trim="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <!--  弹框底部按钮-->
    <template #footer>
      <div>
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 引入自定义枚举文件
import { enterpriseAdd, enterpriseEdit } from '@/api/companysList'
export default {
  props: {
    totalData: {
      type: true
    }
  },
  data () {
    return {
      isShow: false, // 显示弹框
      mode: 'add', // 弹框模式
      //   表单数据
      form: {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      },
      //   表单验证
      rules: {
        eid: [
          { required: true, message: '必填', trigger: 'change' },
          {
            // 自定义验证
            validator: (rule, value, callback) => {
              let bol = false
              //   新增弹框 - 企业编码验证重复数据
              if (this.mode === 'add') {
                bol = this.totalData.every(item => item.eid !== value)
                //   编辑弹框 - 企业编码验证重复数据
              } else if (this.mode === 'edit') {
                const arr = this.totalData.filter(
                  item => item.id !== this.form.id
                )
                bol = arr.every(item => item.eid !== value)
              }
              bol ? callback() : callback(new Error('企业编码重复'))
            },
            trigger: 'change'
          }
        ],
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            // 自定义验证
            validator: (rule, value, callback) => {
              let bol = false
              //   新增弹框 - 企业名称验证重复数据
              if (this.mode === 'add') {
                bol = this.totalData.every(item => item.name !== value)
                //   编辑弹框 - 企业名称验证重复数据
              } else if (this.mode === 'edit') {
                const findItem = this.totalData.filter(
                  item => item.id !== this.form.id
                )
                bol = findItem.every(item => item.name !== value)
              }
              bol ? callback() : callback(new Error('企业名称重复'))
            },
            trigger: 'change'
          }
        ],
        short_name: [{ required: true, message: '必填', trigger: 'change' }],
        intro: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 提交功能
    submit () {
      this.$refs.form.validate(async result => {
        console.log(result)
        if (result) {
          // 新增提交
          if (this.mode === 'add') {
            const res = await enterpriseAdd(this.form)
            console.log(res)
            //   提示
            this.$message.success('添加成功')
            //   编辑提交
          } else if (this.mode === 'edit') {
            const res = await enterpriseEdit(this.form)
            console.log(res)
            //   提示
            this.$message.success('编辑成功')
          }
          //   关闭
          this.isShow = false
          //   刷新列表
          this.$emit('getData')
          //   刷新全部数据列表
          this.$emit('getTotalData')
        }
      })
    },
    // 弹框关闭-统一处理
    closeEvent () {
      this.$refs.form.resetFields()
      //   手动清空数据
      this.form = {
        eid: '',
        name: '',
        short_name: '',
        intro: '',
        remark: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__title {
  color: #fff;
}
::v-deep .el-dialog__header {
  background: linear-gradient(left, #02c3fa, #1294fa);
}
</style>
